/*
See the reference folder for the library to get a deeper explanation of the methods and their parameters.

Relevant methods:

configShakeEvent - When to reigster a shake

getDULData - Get the raw data from DUL Radios

getTiltPosition - Get the 360 degree tilt position from a DUL Radio

setSmoothSamples - How many samples to smooth over

smoothValues - Get smooth value from adc or acc

shake - An event method that has to be insert into the Processing sketch. Is fired when a shake is registered

*/

import processing.serial.*;
import dulRadio.*;

DULRadio dul;
CurveGraph cg;

void setup(){
  size(800,800);
  
  //Exchange COM5 with whatever name your port has. You could use Serial.list()[0]
  dul= new DULRadio(this, "COM5");
  
  //Calibrate when to register shake (see reference for more on the parameters)
  dul.configShakeEvent(1,150, 4,200,500);
  
  //Set how many samples to make an average over
  dul.setSmoothSamples(1,50);
  
  cg=new CurveGraph(10,40, 290, 255, color(123,123,123),100);
}

void draw(){
  background(255,255,255);
  fill(0,0,0);
  
  Integer[] radiovals=dul.getCurrentData(1);
  Integer[] radiovals2=dul.getCurrentData(2);
  
  text("Raw data",300,20);
  text("ID: 1",300,40);
  text("ADC 1: "+radiovals[0],300,60);
  text("ADC 2: "+radiovals[1],300,80);
  text("ACC X: "+radiovals[2],300,100);
  text("ACC Y: "+radiovals[3],300,120);
  text("ACC Z: "+radiovals[4],300,140);
  
  text("Raw data",500,20);
  text("ID: 2",500,40);
  text("ADC 1: "+radiovals2[0],500,60);
  text("ADC 2: "+radiovals2[1],500,80);
  text("ACC X: "+radiovals2[2],500,100);
  text("ACC Y: "+radiovals2[3],500,120);
  text("ACC Z: "+radiovals2[4],500,140);
  
  int[] accy=dul.smoothValues(1);
  cg.addValue(accy[4]);
  cg.draw();
  
  ellipse(350,200,100,100);
  int degree=dul.getTiltPosition(1)[2];
  ellipse(350+50*cos(radians(degree)),200+50*sin(radians(degree)),10,10);
}

//Only working when there is 1 sensor board connected. Otherwise arbitrary datas
void shake(DULRadio d){
  text("IS SHAKING", 10,20);
}

/******************************/
/* Helper code for drawing */
/******************************/

class CurveGraph{
  LinkedList drawList;
  int offsetX;
  int offsetY;
  int rangeX;
  int rangeY;
  float factorScale;
  color graphc;
  int listSize;

  CurveGraph(int xoffset,int yoffset, int xrange, int yrange, color gcol, int lsize) {
    offsetX=xoffset;
    offsetY=yoffset;
    rangeY=yrange;
    rangeX=xrange;
    drawList=new LinkedList();
    graphc=gcol;
    listSize=lsize;
    factorScale=(float)rangeX/listSize;
  }
  
   CurveGraph(color gcol) {
      this(0,0,0,0,gcol,50);
  }
  
  void setParams(int x, int y,int wid, int hei){
    offsetX=x;
    offsetY=y;
    rangeX=wid;
    rangeY=hei;
    factorScale=(float)rangeX/listSize;
  }

  void draw(){
      float tempXOffset=offsetX;
      noFill();
      strokeWeight(3);  
      stroke(graphc);
      beginShape();
      for (int i=0;i<drawList.size();i++) {
        curveVertex(tempXOffset, map((Integer)drawList.get(i),-255,255,offsetY+rangeY,offsetY));
        tempXOffset+=factorScale;
      }
      endShape();
  }

  void addValue(int v){
    drawList.add(v);
    if(drawList.size()>listSize)
      drawList.removeFirst();
  }
}
